package org.skipper.douyin.util;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.nio.ByteBuffer;

@ServerEndpoint("/websocket")
public class MyWebSocketEndpoint {

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("WebSocket opened: " + session.getId());
    }

    @OnMessage
    public void onBinaryMessage(ByteBuffer byteBuffer, Session session) {
        byte[] data = byteBuffer.array();
        System.out.println("Received binary data with length: " + data.length);

        // 假设在这里有处理接收到的二进制数据的逻辑
        // 这里只是简单地将收到的数据原样返回给客户端
//        try {
//            session.getBasicRemote().sendBinary(byteBuffer);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println("WebSocket closed: " + session.getId());
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        System.out.println("WebSocket error: " + throwable.getMessage());
    }
}
